package net.yebaihe.wc;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Search extends StateMachine{
	EditText edtsearch;
	@Override
	protected String getTopbarTitle() {
		return "搜索";
	}

	@Override
	protected void prepareClient(View client) {
		SQLiteDatabase db = MyHelper.get(Search.this).getReadableDatabase();
		
		edtsearch = (EditText)client.findViewById(R.id.edtsearch);
		edtsearch.setOnEditorActionListener(new OnEditorActionListener(){
			@Override
			public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
				if (arg2!=null){
					getWCAppContext().otherRestaurantSql="and name like '%"+edtsearch.getText().toString()+"%' or addr like '%"+edtsearch.getText().toString()+"%'";
					changeToActivity(NearBy.class);
					return true;
				}
				return false;
			}
		});

		TableRow rSubzoneHot1=(TableRow) client.findViewById(R.id.rsubzonehot1);
		TableRow rSubzoneHot2=(TableRow) client.findViewById(R.id.rsubzonehot2);
		Cursor c=db.rawQuery("select * from subzone order by ordertimes desc limit 0,7", null);
		while (c.moveToNext()){
			LinearLayout t=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
			t.setTag(c.getString(0));
			TextView v=(TextView) t.findViewById(R.id.txthot);
			v.setText(c.getString(1));
			t.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					getWCAppContext().otherRestaurantSql="and subzone="+arg0.getTag();
					changeToActivity(NearBy.class);
				}
			});
			if (c.getPosition()<4){
				rSubzoneHot1.addView(t);
			}
			else{
				rSubzoneHot2.addView(t);
			}
		}
		LinearLayout more=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
		more.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				changeToActivity(AllZone.class);
			}
		});
		TextView vmore=(TextView) more.findViewById(R.id.txthot);
		vmore.setText("更多");
		if (c.getCount()<3){
			rSubzoneHot1.addView(more);
		}
		else{
			rSubzoneHot2.addView(more);
		}
		c.close();
	
		TableRow rstylehot1=(TableRow) client.findViewById(R.id.rstylehot1);
		TableRow rstylehot2=(TableRow) client.findViewById(R.id.rstylehot2);
		c=db.rawQuery("select * from style order by ordertimes desc limit 0,7", null);
		while (c.moveToNext()){
			LinearLayout t=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
			t.setTag(c.getString(0));
			t.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					getWCAppContext().otherRestaurantSql="and _id in (select distinct restaurant from style_restaurant where style="+arg0.getTag()+")";
					changeToActivity(NearBy.class);
				}
			});
			TextView v=(TextView) t.findViewById(R.id.txthot);
			v.setText(c.getString(1));
			if (c.getPosition()<4){
				rstylehot1.addView(t);
			}
			else{
				rstylehot2.addView(t);
			}
		}
		more=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
		vmore=(TextView) more.findViewById(R.id.txthot);
		vmore.setText("更多");
		more.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				changeToActivity(AllStyle.class);
			}
		});
		if (c.getCount()<3){
			rstylehot1.addView(more);
		}
		else{
			rstylehot2.addView(more);
		}
		c.close();

		TableRow rsubwayhot1=(TableRow) client.findViewById(R.id.rsubwayhot1);
		TableRow rsubwayhot2=(TableRow) client.findViewById(R.id.rsubwayhot2);
		c=db.rawQuery("select * from subway order by ordertimes desc limit 0,7", null);
		while (c.moveToNext()){
			LinearLayout t=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
			t.setTag(c.getString(0));
			t.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View arg0) {
					getWCAppContext().otherRestaurantSql="and _id in (select distinct restaurant from subway_restaurant where subway="+arg0.getTag()+")";
					changeToActivity(NearBy.class);
				}
			});
			TextView v=(TextView) t.findViewById(R.id.txthot);
			v.setText(c.getString(1));
			if (c.getPosition()<4){
				rsubwayhot1.addView(t);
			}
			else{
				rsubwayhot2.addView(t);
			}
		}
		more=(LinearLayout) View.inflate(this, R.layout.layouthot, null);
		vmore=(TextView) more.findViewById(R.id.txthot);
		vmore.setText("更多");
		more.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				changeToActivity(AllSubway.class);
			}
		});
		if (c.getCount()<3){
			rsubwayhot1.addView(more);
		}
		else{
			rsubwayhot2.addView(more);
		}
		c.close();
		
		db.close();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    }

	@Override
	protected int getLayoutRes() {
		return R.layout.search;
	}

}
